﻿using System;
using Csla;
using Csla.Serialization;

namespace Validation
{
    [Serializable()]
    public class CheckExistService : BusinessBase<CheckExistService>
    {
        #region Business methods
        private static PropertyInfo<bool> IsExistProperty = RegisterProperty<bool>(p => p.IsExist, "IsExist");
        public bool IsExist
        {
            get { return GetProperty(IsExistProperty); }
            set { SetProperty(IsExistProperty, value); }
        }

        private static PropertyInfo<string> ErrorMessageProperty = RegisterProperty<string>(p => p.ErrorMessage, "ErrorMessage");
        public string ErrorMessage
        {
            get { return GetProperty(ErrorMessageProperty); }
            set { SetProperty(ErrorMessageProperty, value); }
        }							
        #endregion

        #region  Factory Methods
        /// <summary>
        /// Check exist of record in database
        /// Use for check unique validation
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="fieldName"></param>
        /// <param name="fieldType"></param>
        /// <param name="fieldValue"></param>
        /// <returns>Use IsExist property to determine the exist of record in database </returns>
        public static void CheckExist(string tableName, string fieldName, string fieldType, string fieldValue, EventHandler<DataPortalResult<CheckExistService>> callback)
        {
            var dp = new DataPortal<CheckExistService>();
            dp.FetchCompleted += callback;
            dp.BeginFetch(new CheckExistServiceCriteria(tableName, fieldName, fieldType, fieldValue), callback);
        }

#if !SILVERLIGHT
        /// <summary>
        /// Check exist of record in database
        /// Use for check unique validation
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="fieldName"></param>
        /// <param name="fieldType"></param>
        /// <param name="fieldValue"></param>
        /// <returns>Use IsExist property to determine the exist of record in database </returns>
        public static CheckExistService CheckExist(string tableName, string fieldName, string fieldType, string fieldValue)
        {
            return DataPortal.Fetch<CheckExistService>(new CheckExistServiceCriteria(tableName, fieldName, fieldType, fieldValue));
        }

        private CheckExistService()
        { /* require use of factory methods */ }

#else
        public CheckExistService()
        { /* require by MobileFormatter */ }
#endif

        #endregion

        #region Root object data access
#if !SILVERLIGHT
        private void DataPortal_Fetch(CheckExistServiceCriteria criteria)
        {
            try
            {
                using (BypassPropertyChecks)
                {
                    string sqlCommand = "Select 1 from " + criteria.TableName + " where " + criteria.FieldName + " = ";
                    if (criteria.FieldType == "int" || criteria.FieldType == "double" || criteria.FieldType == "float" || criteria.FieldType == "decimal")
                    {
                        sqlCommand += criteria.FieldValue;
                    }
                    else if (criteria.FieldType == "string" || criteria.FieldType == "system.datetime?")
                    {
                        sqlCommand += "N'" + criteria.FieldValue + "'";
                    }

                    LoadProperty(IsExistProperty, csla4skeleton.DataAcess.ExecuteSQLService.HasRowsExecuteQueryReader(sqlCommand));
                }
            }
            catch (Exception ex)
            {
                LoadProperty(ErrorMessageProperty, ex.StackTrace);
            }
        }
#endif
        #endregion
    }
}
